package com.msb.mall.product.controller;

import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;
import com.msb.mall.product.entity.AttrEntity;
import com.msb.mall.product.entity.AttrGroupEntity;
import com.msb.mall.product.entity.vo.AttrGroupRelationVo;
import com.msb.mall.product.entity.vo.AttrGroupWithAttrsVo;
import com.msb.mall.product.service.AttrGroupService;
import com.msb.mall.product.service.AttrService;
import com.msb.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;





/**
 * 属性分组
 *
 * @author lwq
 * @email 1907326957@qq.com
 * @date 2024-06-12 21:28:32
 */
@RestController
@RequestMapping("product/attrGroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;


    /**
     * 根据三级分类id获取对应的属性组和属性组的属性信息
     * @param cateLogId 分类id
     * @return
     */
    @GetMapping("/{cateLogId}/withAttr")
    public R getAttrGroupWithAttrs(@PathVariable("cateLogId")Long  cateLogId){
        List<AttrGroupWithAttrsVo> attrsVos = attrGroupService.getAttrGroupWithAttrs(cateLogId);
        return R.ok().put("data",attrsVos);
    }

    /**
     * 保存关联的数据
     * @param vos 关联的数据集合
     * @return
     */
    @PostMapping("/attr/relation")
    public R attrBatchRelation(@RequestBody() List<AttrGroupRelationVo> vos){
        attrService.attrBatchRelation(vos);
        return R.ok();
    }

    /**
     * 根据属性组的id查询出未被关联的属性信息
     * @param attrGroupId 属性组的id
     * @param params 查询条件
     * @return
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String,Object> params){
        PageUtils pageUtils = attrService.getAttrNoRelation(attrGroupId,params);
        return R.ok().put("page",pageUtils);
    }
    /**
     * 删除 属性组和属性关联表中的记录
     * @param vos
     * @return
     */
    @RequestMapping("attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteAttrRelation(vos);
        return R.ok();
    }
    /**
     * 根据属性组id获取基本信息
     * @param attrGroupId 属性组id
     * @return
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> attrList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data",attrList);
    }
    /**
     * 列表
     * 分页查询
     * 前端请求需要封装对应的分页数据
     * {
     *     page: 1 //当前页
     *     limit: 10 //每页显示的条数
     *     sidx: "id" //排序的字段
     *     order: "asc/desc" //排序的方式
     *     key: "小米" //查询的关键字
     * }
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catId") Long catId){
        params.put("catId",catId);
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //拼接对应的类别id[一级，二级，三级]
        Long cateLogId = attrGroup.getCatelogId();
        Long[] cateLogIds =  categoryService.findCateLogIds(cateLogId);
        attrGroup.setCatelogIds(cateLogIds);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
